package com.spboot.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.spboot.entity.Product;
import com.spboot.page.PageInfo;
import com.spboot.service.ProductService;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author guocb
 * @since 2020-01-13
 */
@Controller
@RequestMapping("/product")
public class ProductController {

	@Autowired
	ProductService productService;

	@RequestMapping("/list")
	public String list(String proName, @RequestParam(defaultValue = "1", required = false) Integer pageno,
			Model model) {
		if (StringUtils.isNoneEmpty(proName) && proName.indexOf(" ") != -1) {
			proName = proName.replaceAll(" ", ",");
		}
		PageInfo<Product> pageInfo = productService.selectProductSolr(proName, pageno);
		model.addAttribute("pageInfo", pageInfo);
		return "prolist";
	}

	@RequestMapping("/toadd")
	public String toadd() {
		return "proadd";
	}

	@RequestMapping("/add")
	public String add(Product product) {
		boolean f = productService.saveOrUpdateProduct(product);
		if (f) {
			return "redirect:list";
		}
		return "proadd";
	}

	@ResponseBody
	@RequestMapping("/del/{id}")
	public Boolean delete(@PathVariable Integer id) {
		return productService.deleteProductById(id);
	}
}
